﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class UptinDAO
    {
        public static List<UpTinDTO> getAllOtherDate()
        {
            List<UpTinDTO> result = new List<UpTinDTO>();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DateTime nowDate = DateTime.Now;
            var query = dbRaoVat.UPTINs.Where(dm => nowDate<=dm.ThoiGianKetThuc ).ToList();

            foreach (var ds in query)
            {
                UpTinDTO uptin = new UpTinDTO();
                uptin.KhoangThoiGianTuDongUp = ds.KhoangThoiGianTuDongUp.Value;
                uptin.MaTinRaoVat = ds.MaTinRaoVat.Value;
                uptin.ThoiGianBatDau = ds.ThoiGianBatDau.Value;
                uptin.ThoiGianKetThuc = ds.ThoiGianKetThuc.Value;

                result.Add(uptin);
            }

            return result;
        }

        public static bool isDuocPhepUp(int maTinRaoVat)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            DateTime now = DateTime.Now;
            var query = dbRaoVat.UPTINs.Where(dm => dm.MaTinRaoVat==maTinRaoVat &&
                now <= dm.ThoiGianKetThuc).ToList();

            if (query.Count > 0)
                return false;
            else
                return true;
        }

        public static bool Insert(UpTinDTO _insert)
        {
            bool result = true;
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            UPTIN uptin = new UPTIN();
            uptin.KhoangThoiGianTuDongUp = _insert.KhoangThoiGianTuDongUp;
            uptin.MaTinRaoVat = _insert.MaTinRaoVat;
            uptin.ThoiGianBatDau = _insert.ThoiGianBatDau;
            uptin.ThoiGianKetThuc = _insert.ThoiGianKetThuc;

            try
            {
                dbRaoVat.UPTINs.InsertOnSubmit(uptin);
                dbRaoVat.SubmitChanges();
            }
            catch (Exception)
            {
                result = false;
            }
            return result;
        }

        /*
        public static TreatasDTO getAlongCode(int maTreatas)
        {
            TreatasDTO result = new TreatasDTO();
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            var query = dbRaoVat.TREATAs.Where(dm => dm.Deleted.Equals(0) &&
                             dm.MaTreatAs == maTreatas).Single();

            result.MaTreatAs = query.MaTreatAs;
            result.TreatAsName = query.TreatAsName; 

            return result;
        }

        

        public static void Delete(int maTreatas)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TREATA _delete = (from n in dbRaoVat.TREATAs
                             where n.MaTreatAs == maTreatas && n.Deleted == 0
                             select n).Single();

            _delete.Deleted = 1;
            dbRaoVat.SubmitChanges();
        }

        public static void Update(TreatasDTO dto)
        {
            RaoVatDataContext dbRaoVat = new RaoVatDataContext();

            TREATA _update = (from n in dbRaoVat.TREATAs
                              where n.MaTreatAs == dto.MaTreatAs && n.Deleted == 0
                              select n).Single();

            _update.TreatAsName = dto.TreatAsName;
            
            dbRaoVat.SubmitChanges();
        }*/
    }
}
